<?php
/*
 * @Author: lokei
 * @Date: 2022-10-12 22:09:29
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-28 09:14:07
 * @Description: 
 */
namespace App\Http\Controllers\Cms\Poster;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Cms\Poster\PosterModel;
use App\Models\Store\StoreModel;
use Illuminate\Http\Request;

class PosterController extends Controller {
    public function list() {
        $items = PosterModel::get();
        $res = ResultTool::success();
        $res['data']['items'] = $items;
        $res['data']['total'] = count($items);
        return $res;
    }

    public function get(Request $request) {
        $poster = PosterModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $poster;
        return $res;
    }

    public function ofType(Request $request) {
        $res = ResultTool::success();
        if($request->input('type') == '5') {
            $poster = PosterModel::where('type', '=', '5')->where('store_id', '=', $request->input('store_id'))->first();
            $res['data'] = $poster;
        }
        return $res;
    }

    public function create(Request $request) {
        $poster = new PosterModel();
        $poster->type = $request->input('type');
        if($request->input('type') == '5') {
            $poster->store_id = $request->input('store_id');
            $store = StoreModel::where('id', '=', $request->input('store_id'))->first();
            $poster->store_name = $store->name;
        }
        $poster->bg_img = $request->input('bg_img');
        $poster->title = $request->input('title');
        $poster->save();
        return ResultTool::success();
    }
    
    public function update(Request $request) {
        $poster = PosterModel::where('id', '=', $request->input('id'))->first();
        $poster->type = $request->input('type');
        if($request->input('type') == '5') {
            $poster->store_id = $request->input('store_id');
        }
        $poster->bg_img = $request->input('bg_img');
        $poster->title = $request->input('title');
        $poster->save();
        return ResultTool::success();
    }
    
    public function delete(Request $request) {
        PosterModel::where('id', '=', $request->input('id'))->delete();
        return ResultTool::success();
    }
}